/*
 * Copyright 2012-2019 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package smoketest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.event.AbstractApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.validation.annotation.Validated;
import smoketest.event.ApplicationEventTest;
import smoketest.event.ApplicationListenerTest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@SpringBootApplication
@EnableAsync
public class SpringBootTestApplication extends SpringBootServletInitializer {

	public static void main(String[] args) {
		ConfigurableApplicationContext run = SpringApplication.run(SpringBootTestApplication.class, args);
//		run.addApplicationListener(new ApplicationListenerTest());
		//发布事件
		run.publishEvent(new ApplicationEventTest(new Object(), "测试事件."));
	}

	//	@Bean(name = "asyncExecutor")
//	public ThreadPoolTaskExecutor executor(SimpleApplicationEventMulticaster applicationEventMulticaster) {
//		ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
//		//线程池大小
//		taskExecutor.setCorePoolSize(10);
//		//线程池最大线程数
//		taskExecutor.setMaxPoolSize(20);
//		//最大等待任务数
//		taskExecutor.setQueueCapacity(25);
//		taskExecutor.setThreadNamePrefix("INS-ASYNC-");
//		taskExecutor.initialize();
//		applicationEventMulticaster.setTaskExecutor(taskExecutor);
//		return taskExecutor;
//	}
//	@Bean
//	public TomcatServletWebServerFactory tomcatServletWebServerFactory() {
//		TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
//		factory.setPort(8083);
//		return factory;
//	}

	@Bean
	public WebServerFactoryCustomizer webServerFactoryCustomizer() {
		WebServerFactoryCustomizer webServerFactoryCustomizer = new WebServerFactoryCustomizer() {
			@Override
			public void customize(WebServerFactory factory) {
				System.out.println(factory.getClass());
				TomcatServletWebServerFactory factory1 =( TomcatServletWebServerFactory)factory;
				factory1.setPort(8083);
			}
		};

		return webServerFactoryCustomizer;
	}

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return super.configure(builder).sources(SpringBootTestApplication.class);
	}
}
